package com.shj.em.biz.finance.controller;

import com.shj.em.aa.entity.User;
import com.shj.em.base.controller.BaseController;
import com.shj.em.base.support.ResMsg;
import com.shj.em.biz.Constants;
import com.shj.em.biz.company.entity.Company;
import com.shj.em.biz.finance.dto.OrderPaymentDTO;
import com.shj.em.biz.finance.entity.Payment;
import com.shj.em.biz.finance.service.PaymentService;
import com.shj.em.biz.sell.dto.OrderInitDTO;
import com.shj.em.biz.sell.entity.Order;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.sys.service.CdService;
import com.shj.em.common.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 付款信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-13
 */
@RestController
@Slf4j
@RequestMapping("/finance/payment")
public class PaymentController extends BaseController<Payment>{

    @Autowired
    PaymentService paymentService;


    @GetMapping("/loadInitPageData")
    public ResMsg<OrderInitDTO> loadInitPageData(){
        OrderInitDTO initDTO = new OrderInitDTO();

        List<Company> companyList = companyService.getAllCustomers();

        List<User> sells = userService.getUsersByRoleId(Constants.ROLE_SELL);
        initDTO.setSells(sells);

        initDTO.setCustomers(companyList);

        List<Cd> statusList = cdService.queryCdsByType(CdService.CD_ORDER_STATUS);
        initDTO.setStatusList(statusList);

        return ResMsg.ok(initDTO);
    }

    @GetMapping("/{orderId}")
    public ResMsg<OrderPaymentDTO> findOrderById(@PathVariable("orderId") Long orderId){
        OrderPaymentDTO initDTO = new OrderPaymentDTO();
        Order order = orderService.selectById(orderId);
        initDTO.setOrder(order);

        List<Payment> list = paymentService.getPaymentsByOrderId(orderId);
        initDTO.setPaymentList(list);

        return ResMsg.ok(initDTO);
    }

    @GetMapping("/findPaymentsByOrderId/{orderId}")
    public ResMsg<List<Payment>> findPaymentsByOrderId(@PathVariable("orderId") Long orderId){
        List<Payment> list = paymentService.getPaymentsByOrderId(orderId);
        return ResMsg.ok(list);
    }

    @Log("批量删除付款")
    @DeleteMapping
    public ResMsg batchRemove(@RequestBody Long[] ids){
        paymentService.deletePaymentByIds(ids);
        return ResMsg.ok();
    }

    @Log("创建付款信息")
    @PostMapping
    public ResMsg<Cd> add(@RequestBody Payment payment){
        try{
            BigDecimal unpaid = paymentService.getUnpaidAmount(payment.getOrderId(), null);
            if(payment.getPaid().compareTo(unpaid) == 1){
                return ResMsg.error("超出应付款总额。请检查！");
            }
            Cd cd = paymentService.createPayment(payment);
            return ResMsg.ok(cd);
        }catch(Exception e){
            log.error("创建付款信息失败", e);
            return ResMsg.error("创建付款信息失败");
        }

    }

    @Log("更新付款信息")
    @PutMapping
    public ResMsg<Cd> edit(@RequestBody Payment payment){
        try{
            BigDecimal unpaid = paymentService.getUnpaidAmount(payment.getOrderId(), payment.getId());
            if(payment.getPaid().compareTo(unpaid) == 1){
                return ResMsg.error("超出应付款总额。请检查！");
            }
            Cd cd = paymentService.updatePayment(payment);
            return ResMsg.ok(cd);
        }catch(Exception e){
            log.error("更新付款信息失败", e);
            return ResMsg.error("更新付款信息失败");
        }

    }

}

